package com.tian.view.anim.view_anim

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.AlphaAnimation
import android.view.animation.Animation
import android.view.animation.AnimationSet
import android.view.animation.AnimationUtils
import android.view.animation.RotateAnimation
import android.view.animation.ScaleAnimation
import android.view.animation.TranslateAnimation
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.tian.view.R
import com.tian.view.databinding.ViewFourBottonBinding

/**
 * @author tiansongtao, Email:tiansongtao@up366.com
 * @date 2022/12/14 10:04
 *
 * set集合动画
 */
class SetAnimFragment : Fragment() {

    private lateinit var b: ViewFourBottonBinding


    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        b = ViewFourBottonBinding.inflate(inflater)
        return b.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        (requireActivity() as AppCompatActivity).supportActionBar?.title = "Set集合动画"
        b.mbtOne.text = "xml定义Set集合动画"

        val listener = object : Animation.AnimationListener {
            override fun onAnimationStart(animation: Animation?) {
                Toast.makeText(requireContext(), "anim start", Toast.LENGTH_SHORT).show()
            }

            override fun onAnimationEnd(animation: Animation?) {
                Toast.makeText(requireContext(), "anim end", Toast.LENGTH_SHORT).show()
            }

            override fun onAnimationRepeat(animation: Animation?) {
                Toast.makeText(requireContext(), "anim repeat", Toast.LENGTH_SHORT).show()
            }
        }

        // AnimationUtils可以加载 xml 配置动画
        val anim = AnimationUtils.loadAnimation(requireContext(), R.anim.set_anim_1)
        anim.setAnimationListener(listener)
        b.mbtOne.setOnClickListener {
            b.mbtOne.startAnimation(anim)
        }

        b.mbtTwo.text = "java代码Set集合"
        b.mbtTwo.setOnClickListener {
            val alphaAnim = AlphaAnimation(0.0f, 1.0f)
            val rotateAnim = RotateAnimation(0f, 270f, 50f, 50f)
            val scaleAnim = ScaleAnimation(0.5f, 1.5f, 0.4f, 1.4f,
                    Animation.RELATIVE_TO_PARENT, 0.2f, Animation.ABSOLUTE, 20f)
            val translateAnim = TranslateAnimation(Animation.ABSOLUTE, 0f, Animation.RELATIVE_TO_SELF, 0.2f,
                    Animation.ABSOLUTE, 0f, Animation.RELATIVE_TO_PARENT, -0.2f)
            val setAnim = AnimationSet(true)
            setAnim.addAnimation(alphaAnim)
            setAnim.addAnimation(rotateAnim)
            setAnim.addAnimation(scaleAnim)
            setAnim.addAnimation(translateAnim)
            setAnim.duration = 3000L
            setAnim.setAnimationListener(listener)
            b.mbtTwo.startAnimation(anim)
        }
        b.mbtThree.visibility = View.GONE
        b.mbtFour.visibility = View.GONE
    }


}